<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ApplicationModel extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];

    public function time_ago_in_words($field = 'created_at')
    {
        $carbon = new Carbon($this->{$field});
        $string = $carbon->diffForHumans(null, true);
        $x = array(
            'years' => '年',
            'year' => '年',
            'months' => '个月',
            'month' => '个月',
            'days' => '天',
            'day' => '天',
            'weeks' => '周',
            'week' => '周',
            'hours' => '小时',
            'hour' => '小时',
            'minutes' => '分钟',
            'minute' => '分钟',
            'seconds' => '秒',
            'second' => '秒'
        );
        $string = str_replace(array_keys($x), array_values($x), $string);
        $string = str_replace(" ", "", $string);
        return $string . "前";
    }

    public function pk_cache_key($suffix = null)
    {
        $version = '2016.12.09';
        $key = $this->model_name() . '_' . $this->id . '_' . (string)$this->updated_at->timestamp;
        if ($suffix != null) {
            $key = $version . '_' . $key . '_' . $suffix;
        }
        return md5($key);
    }

    /***
     * @return string short name of current model name
     */
    public function model_name()
    {
        return (new \ReflectionClass($this))->getShortName();
    }

    /**
     * @return mixed
     */
    public function img_host()
    {
        return env('IMG_HOST');
    }

    /**
     * @return mixed
     */
    public static function static_img_host()
    {
        return env('IMG_HOST');
    }
}
